package gzhaoku.com.common.ui.components;

import android.os.Build;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.net.URLEncoder;

import cz.msebera.android.httpclient.HttpStatus;

public class XWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return true;
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(request.isForMainFrame()){
                view.loadUrl("file:///android_asset/web/error/index.html?target="+ URLEncoder.encode(request.getUrl().toString()));
                return;
            }
        }

        super.onReceivedError(view, request, error);
    }
}
